/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Interactiontype;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Interaction;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class InteractiontypeJpaController {

    public InteractiontypeJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

     public Interactiontype save(Interactiontype t) {
        Interactiontype aux = findInteractiontype(t.getInteractiontype());
        if(aux==null){
            System.out.println("Interaction type "+t.getInteractiontype()+" cannot be inserted in database.");
            return null;
        } else
            return aux;
    }

    public Interactiontype create(Interactiontype interactiontype) {
        Interactiontype r = null;
        if (interactiontype.getInteractionList() == null) {
            interactiontype.setInteractionList(new ArrayList<Interaction>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Interaction> attachedInteractionList = new ArrayList<Interaction>();
            for (Interaction interactionListInteractionToAttach : interactiontype.getInteractionList()) {
                interactionListInteractionToAttach = em.getReference(interactionListInteractionToAttach.getClass(), interactionListInteractionToAttach.getIdinteraction());
                attachedInteractionList.add(interactionListInteractionToAttach);
            }
            interactiontype.setInteractionList(attachedInteractionList);
            r = em.merge(interactiontype); //em.persist(interactiontype);
            for (Interaction interactionListInteraction : interactiontype.getInteractionList()) {
                Interactiontype oldInteractiontypeOfInteractionListInteraction = interactionListInteraction.getInteractiontype();
                interactionListInteraction.setInteractiontype(interactiontype);
                interactionListInteraction = em.merge(interactionListInteraction);
                if (oldInteractiontypeOfInteractionListInteraction != null) {
                    oldInteractiontypeOfInteractionListInteraction.getInteractionList().remove(interactionListInteraction);
                    oldInteractiontypeOfInteractionListInteraction = em.merge(oldInteractiontypeOfInteractionListInteraction);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Interactiontype interactiontype) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Interactiontype persistentInteractiontype = em.find(Interactiontype.class, interactiontype.getIdinteractiontype());
            List<Interaction> interactionListOld = persistentInteractiontype.getInteractionList();
            List<Interaction> interactionListNew = interactiontype.getInteractionList();
            List<Interaction> attachedInteractionListNew = new ArrayList<Interaction>();
            for (Interaction interactionListNewInteractionToAttach : interactionListNew) {
                interactionListNewInteractionToAttach = em.getReference(interactionListNewInteractionToAttach.getClass(), interactionListNewInteractionToAttach.getIdinteraction());
                attachedInteractionListNew.add(interactionListNewInteractionToAttach);
            }
            interactionListNew = attachedInteractionListNew;
            interactiontype.setInteractionList(interactionListNew);
            interactiontype = em.merge(interactiontype);
            for (Interaction interactionListOldInteraction : interactionListOld) {
                if (!interactionListNew.contains(interactionListOldInteraction)) {
                    interactionListOldInteraction.setInteractiontype(null);
                    interactionListOldInteraction = em.merge(interactionListOldInteraction);
                }
            }
            for (Interaction interactionListNewInteraction : interactionListNew) {
                if (!interactionListOld.contains(interactionListNewInteraction)) {
                    Interactiontype oldInteractiontypeOfInteractionListNewInteraction = interactionListNewInteraction.getInteractiontype();
                    interactionListNewInteraction.setInteractiontype(interactiontype);
                    interactionListNewInteraction = em.merge(interactionListNewInteraction);
                    if (oldInteractiontypeOfInteractionListNewInteraction != null && !oldInteractiontypeOfInteractionListNewInteraction.equals(interactiontype)) {
                        oldInteractiontypeOfInteractionListNewInteraction.getInteractionList().remove(interactionListNewInteraction);
                        oldInteractiontypeOfInteractionListNewInteraction = em.merge(oldInteractiontypeOfInteractionListNewInteraction);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = interactiontype.getIdinteractiontype();
                if (findInteractiontype(id) == null) {
                    throw new NonexistentEntityException("The interactiontype with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Interactiontype interactiontype;
            try {
                interactiontype = em.getReference(Interactiontype.class, id);
                interactiontype.getIdinteractiontype();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The interactiontype with id " + id + " no longer exists.", enfe);
            }
            List<Interaction> interactionList = interactiontype.getInteractionList();
            for (Interaction interactionListInteraction : interactionList) {
                interactionListInteraction.setInteractiontype(null);
                interactionListInteraction = em.merge(interactionListInteraction);
            }
            em.remove(interactiontype);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Interactiontype> findInteractiontypeEntities() {
        return findInteractiontypeEntities(true, -1, -1);
    }

    public List<Interactiontype> findInteractiontypeEntities(int maxResults, int firstResult) {
        return findInteractiontypeEntities(false, maxResults, firstResult);
    }

    private List<Interactiontype> findInteractiontypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Interactiontype.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Interactiontype findInteractiontype(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Interactiontype.class, id);
        } finally {
            em.close();
        }
    }

    public Interactiontype findInteractiontype(String q) {
        Query query= getEntityManager().createNamedQuery("Interactiontype.findByInteractiontype");
        query.setParameter("interactiontype",q);

        return query.getResultList().isEmpty()?null:(Interactiontype)query.getResultList().get(0);
    }

    public int getInteractiontypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Interactiontype> rt = cq.from(Interactiontype.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
